package com.evola.dbsm.view.project.edit
{
	import com.evola.dbsm.model.DatabaseBean;
	import com.evola.dbsm.model.ProjectBean;
	import com.evola.dbsm.model.UserBean;
	import com.evola.dbsm.utils.ListUtils;

	import mx.collections.ArrayCollection;
	import mx.collections.IList;

	[Bindable]
	public class ProjectEditModel
	{

		public var project:ProjectBean;
		public var isInsert:Boolean;

		//trenutno izabrana baza
		public var currentDatabase:DatabaseBean;

		//validacija basic info
		public var basicInfoError:String;

		//selektovani index view stack-a
		public var selectedIndex:int=0;

		//linkovi 
		public var links:Array=["Project Info", "Databases", "Members"];

		//baze projekta
		public var databases:IList;

		//validacija za bazu
		public var databaseError:String;

		//clanovi projekta
		public var members:IList;

		//validacija za clanove projekta
		public var membersError:String;

		//trenutno izabrani member
		public var currentMember:UserBean;

		//svi user-i
		public var allUsers:IList;

		//da li se dodaje postojeci user
		public var isAddingExistingUser:Boolean=false;

		//index selektovanog user-a
		public var selectedUserIndex:int=-1;

		public function prepareForInsertion():void
		{

			var project:ProjectBean=new ProjectBean();

			project=project;
			isInsert=true;
		}

		public function validateBasicInfo():void
		{

			if (!project.name || !project.id)
				basicInfoError="You must enter project name and id.";
			else
				basicInfoError=null;
		}

		public function validateDatabase():void
		{

			if (currentDatabase)
			{

				if (!currentDatabase.name || !currentDatabase.url || !currentDatabase.driver || !currentDatabase.username)
					databaseError="You must enter all required database information.";
				else
					databaseError=null;
			}
			else
			{

				databaseError="Select database to modify.";
			}

		}

		public function editDatabase(database:DatabaseBean):void
		{

			ListUtils.replaceBean(databases, database);
		}

		public function addDatabase(database:DatabaseBean):void
		{

			if (!databases)
				databases=new ArrayCollection();

			databases.addItem(database);
		}

		public function removeDatabase(database:DatabaseBean):void
		{

			ListUtils.removeBean(databases, database);
		}

		public function validateMember():void
		{

			if (isAddingExistingUser)
			{

				if (selectedUserIndex == -1)
				{
					membersError="You must select a user.";
				}
				else
				{

					var selectedUser:UserBean=allUsers.getItemAt(selectedUserIndex) as UserBean;
					//mozda se user vec nalazi medju userima
					var userIndex:int=ListUtils.getBeanIndex(members, selectedUser);

					if (userIndex >= 0)
						membersError="Selected user is already a member of this project.";
					else
						membersError=null;
				}
			}
			else
			{
				if (currentMember)
				{

					if (!currentMember.name || !currentMember.username || !currentMember.surname || !currentMember.password)
						membersError="You must enter all required member information.";
					else
						membersError=null;
				}
				else
				{

					membersError="Select member to modify.";
				}
			}
		}

		public function editMember(user:UserBean):void
		{

			ListUtils.replaceBean(members, user);
		}

		public function addMember(user:UserBean):void
		{

			if (!members)
				members=new ArrayCollection();

			members.addItem(user);
		}

		public function removeMember(user:UserBean):void
		{

			ListUtils.removeBean(members, user);
		}
	}
}
